/*
 * @Description: 表格设置
 * @Author: DHL
 * @Date: 2021-12-14 10:39:39
 * @LastEditors: DHL
 * @LastEditTime: 2022-03-13 20:05:01
 */

import { defineStore } from 'pinia'
import { store } from '/@/store'
import { storageUtils } from '/@/utils/storageUtils'
import { useLoginUserStore } from '/@/store/modules/loginUser'
import { stringUtils } from '/@/utils/stringUtils'
import { arrayRemoveByPropertyValue } from '/@/utils/twUtils'
import { merge } from 'lodash-es'

const TABLE_SETTING_KEY = 'TW-TABLE-SETTING'

/**
 * 用户配置列
 * key：tableId
 * value：配置列
 */
interface ICustomColumn {
  [key: string]: Array<any>
}

/**
 * 用户表格配置
 */
export interface ITableSettingState {
  /**
   * 配置列
   */
  customColumn: ICustomColumn
}

const tableSettingStore = defineStore({
  id: 'tableSettingState',
  state: (): ITableSettingState => ({
    customColumn: {} as ICustomColumn
  }),
  getters: {},
  actions: {
    /**
     * 获取登录用户
     */
    getLoginName() {
      let loginName = useLoginUserStore().loginName
      if (stringUtils.isEmpty(loginName)) {
        loginName = 'defaultLoginName'
      }
      return loginName
    },

    /**
     * 构建表格配置
     */
    buildTableSettingAction() {
      // 读取缓存配置
      const tableSettingList: Array<any> = storageUtils.getLocalItem(TABLE_SETTING_KEY)
      if (tableSettingList && tableSettingList.length > 0) {
        const loginName = this.getLoginName()

        let tableSetting: any = null

        tableSettingList.forEach((item) => {
          if (item.key === loginName) {
            tableSetting = item
          }
        })

        if (tableSetting) {
          this.setTableSetting(tableSetting.value as ITableSettingState)
        }
      }
    },

    /**
     * 设置配置
     * @param tableSetting
     */
    setTableSetting(tableSetting: ITableSettingState) {
      this.customColumn = tableSetting.customColumn
    },

    /**
     * 列设置
     * @param customColumn
     */
    setCustomColumn(customColumn: ICustomColumn) {
      merge(this.customColumn, customColumn)
      this.saveTableSettingAction()
    },

    /**
     * 获取列设置
     */
    getCustomColumn(tableId: string) {
      return this.customColumn[tableId]
    },

    /**
     * 获取配置
     * @returns
     */
    getTableSetting() {
      return {
        customColumn: this.customColumn
      }
    },

    /**
     * 保存表格配置
     */
    saveTableSettingAction() {
      const loginName = this.getLoginName()

      // 获取浏览器换成配置
      let tableSettingList: Array<any> = storageUtils.getLocalItem(TABLE_SETTING_KEY)

      if (tableSettingList && tableSettingList.length > 0) {
        // 如果配置不为空，删除用户的配置
        arrayRemoveByPropertyValue(tableSettingList, 'key', loginName)

        tableSettingList.push({
          key: loginName,
          value: this.getTableSetting()
        })
      } else {
        tableSettingList = [
          {
            key: loginName,
            value: this.getTableSetting()
          }
        ]
      }

      // 配置缓存搭配浏览器
      storageUtils.setLocalItem({
        key: TABLE_SETTING_KEY,
        value: tableSettingList
      })
    }
  }
})

export function useTableSettingStore() {
  const _tableSettingStore = tableSettingStore(store)
  return _tableSettingStore
}
